$(function () {
    init()
    keyboard_event()
})

// 初始化
function init(type = 0) {
    if (type == 0) {
        get_hero()

        //聚焦
        $(".focus").focus()
    }
    
    layout()
    reflush()
}

// 走路事件分支
function direction_event(direction, type, x, y) {
    if (direction[0].constructor == Number) {
        if (direction[0] == 0) {
            change_space(x, y)
            change_player(type, direction[1], direction[2])
        }
        else if (direction[0] == 2) {
            floor += 1
            init(1)
        }
    }
    else {
        if (direction[0][0] == 0) {
            useItem(direction[0][1], direction[1], direction[2])
        }
        else if (direction[0][0] == 1) {
            battle(direction[0][1], direction[1], direction[2], 0)
        }
        else if (direction[0][0] == 2) {
            battle(direction[0][1], direction[1], direction[2], 1)
        }
    }
}

function useItem(item_id, x, y) {
    var res = {
        "skill": "",
        "equipment": "",
    };
    $.each(hero, function (k, v) {
        if (k == "skill") {
            $.each(v, function (k, vv) {
                res["skill"] += vv["id"] + ","
            });
        }
        else if (k == "equipment") {
            $.each(v, function (k, vv) {
                res["equipment"] += vv["id"] + ","
            });
        }
        else {
            res[k] = v
        }
    })
    res["skill"] = res["skill"].substr(0, res["skill"].length - 1); 
    res["equipment"] = res["equipment"].substr(0, res["equipment"].length - 1); 

    $.ajax({
        method: "POST",
        url: "/api/useItem",
        data: { hero: res, item: item_id },
        dataType: 'json',
        async: false,
        success: function (data) {
            hero["level"] = data.player.level
            hero["exp"] = data.player.exp
            hero["max_exp"] = data.player.max_exp
            hero["hp"] = data.player.hp
            hero["atk"] = data.player.atk
            hero["defense"] = data.player.defense
            hero["hit"] = data.player.hit
            hero["dod"] = data.player.dod
            hero["cri"] = data.player.cri
            hero["skill"] = data.player.skill
            hero["equipment"] = data.player.equipment
            reflush()

            layer.msg(data.use_log)
            change_space(x, y)
        }
    })
}

// 战斗
function battle(monster_id,x, y, type) {
    var res = {
        "skill": "",
        "equipment": "",
    };
    $.each(hero, function (k, v) {
        if (k == "skill") {
            $.each(v, function (k, vv) {
                res["skill"] += vv["id"] + ","
            });
        }
        else if (k == "equipment") {
            $.each(v, function (k, vv) {
                res["equipment"] += vv["id"] + ","
            });
        }
        else {
            res[k] = v
        }
    })
    res["skill"] = res["skill"].substr(0, res["skill"].length - 1); 
    res["equipment"] = res["equipment"].substr(0, res["equipment"].length - 1); 

    $.ajax({
        method: "POST",
        url: "/api/battle",
        data: { hero: res, monster: monster_id },
        dataType: 'json',
        async: false,
        success: function (data) {
            hero["level"] = data.player.level
            hero["exp"] = data.player.exp
            hero["max_exp"] = data.player.max_exp
            hero["hp"] = data.player.hp
            hero["atk"] = data.player.atk
            hero["defense"] = data.player.defense
            hero["hit"] = data.player.hit
            hero["dod"] = data.player.dod
            hero["cri"] = data.player.cri
            hero["skill"] = data.player.skill
            hero["equipment"] = data.player.equipment
            reflush()

            parent.battle_msg(data.battle_log, data.monster);
            $(".focus", parent.document).focus()
            if (data.status == 1) {
                if (type == 0) {
                    change_space(x, y)
                } else {
                    change_stair(x, y)
                }
            } else {
                $("body").html("<p>游戏结束！点击<a href='javascript:;' onclick='window.location.reload()'>重新开始</a></p>")
            }
        }
    })
}


// 获取玩家信息
function get_hero() {
    $.ajax({
        method: "GET",
        url: "/api/get_hero",
        dataType: 'json',
        async: false,
        success: function (data) {
            hero = data
        }
    })
}

// 获取地图信息
function layout() {
    $.ajax({
        method: "GET",
        url: "/api/get_map",
        data: { floor: floor, level: hero["level"] },
        dataType: 'json',
        async: false,
        success: function (data) {
            map = data
            var html = ""
            $.each(data, function (k, v) {
                html += "<div class='line'>"
                $.each(v, function (kk, vv) {
                    if (vv == 1) {
                        html += set_wall(kk, k)
                    } else if (vv == 0) {
                        html += set_space(kk, k)
                    } else if (vv == 2) {
                        html += set_door(kk, k)
                    } else if (vv == 3) {
                        html += set_hero("down", kk, k)
                    } else {
                        if (vv[0] == 0) {
                            html += set_item(vv[1], kk, k)
                        } else if (vv[0] == 1 || vv[0] == 2) {
                            html += set_monster(vv[1], kk, k)
                        }
                    }
                });
                html += "</div>"
            });
            $("#main").html(html)
        }
    });
}

function keyboard_event() {
    // 监控方向键 上38 下40 左37 右39
    $(document).keydown(function (event) {
        event.preventDefault(); // 禁用默认按键事件

        var keyNum = event.which; // 获取按键

        if (keyNum == 40) {
            down_event()
        }
        else if (keyNum == 38) {
            up_event()
        }
        else if (keyNum == 37) {
            left_event()
        }
        else if (keyNum == 39) {
            right_event()
        }
    })
}